<?php
namespace app\admin\model;

use think\facade\Config;
use think\model;


class Order  extends  Model
{
    public function searchKeywordsAttr($query,$value,$data){
        if (''!==$value){
            $query->whereLike('buyer_name','%'.$value.'%');
        }
    }
    public function searchSmallAttr($query,$value){
        if(!empty($value)){
            $query->where('order_amount','>=',$value);
        }
    }

    public function searchBigAttr($query,$value){
        if(!empty($value)){
            $query->where('order_amount','<=',$value);
        }
    }

    public function searchOrderAmountAttr($query,$value){
        if(!empty($value) && is_array($value) && 2 === count($value)){
            $query->whereBetween('order_amount',$value);
        }
    }
    public function searchCidAttr($query,$value,$data){
        if(is_array($value) || (is_scalar($value) &&  strpos('#'.$value,','))){
            $query->whereIn('ecm_order.status',$value);
        }

        if(is_scalar($value) &&  false === strpos($value,',') && intval($value) < 2){
            $query->where('ecm_order.status',intval($value));
        }
    }




    protected $pk = 'order_id';

    public static function getOrderList($data=[],$query=[],$page = false)
    {
        $field = [
            'o.order_id','o.buyer_name','o.postscript','o.status','o.add_time','o.goods_amount',
            's.status_id','s.order_status',
        ];

            $model = self::field($field) 
                ->alias('o')
                ->field($field)
                ->order('add_time desc')
                ->join(['ecm_order_status'=>'s'],'o.status = s.status_id','LEFT')
                ->withSearch(array_keys($data),$data);


//            if (isset($data['keywords']) && ''!==$data['keywords']){
//                $model->whereLike('order_sn','%'.$data['keywords'].'%');
//            }
//
//            if (isset($data['cid']) && is_array($data['cid'])){
//                $model->whereIn('o.status',$data['cid']);
//            }
//            if (isset($data['small']) && ''!==$data['small']){
//                $model->where('order_amount','>=',$data['small']);
//            }
//            if (isset($data['big']) && ''!==$data['big']){
//                $model->where('order_amount','<=',$data['big']);
//            }
             $list_rows = Config::has('view.list_rows')?Config::get('view.list_rows'):5;
            if ($page){
                $result = $model->Select();
            }else{
                $result=$model->paginate([
                   'list_rows'=> $list_rows,
                   'query'     =>$query,
                ]);
            }

         return $result;
    }

}
